﻿using BCL.WPFComponent.Base;
using Devices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace DevicesView.Config
{
    /// <summary>
    /// UCDevicesCollectionConfig.xaml 的交互逻辑
    /// </summary>
    public partial class UCPrintConfigNew : BaseFunctionControl
    {
        public UCPrintConfigNew()
        {
            InitializeComponent();
        }

        public override void LanguageBinding()
        {

        }

        private void Combobox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!(treeView.SelectedItem is PropertyNodeItem item) || item.Tag == null)
                return;

            if (!(item.Tag is DevicesInformation))
                return;

            if (Combobox1.SelectedItem is PrintInfo print)
                pictureBox1.Source = PrintLogic.GetBitImage(PrintLogic.GetPrintConfig(print)?.PrintInfo?.Images[0]);

        }
        private void BaseFunctionControl_Loaded(object sender, RoutedEventArgs e)
        {
            List<PropertyNodeItem> tree = new List<PropertyNodeItem>();
            DevicesCollection.GetCanAddDevices().ForEach(d =>
            {
                PropertyNodeItem node = tree.FirstOrDefault(n => n.Name == d.Brand);
                if (node == null)
                    tree.Add(node = new PropertyNodeItem { IsEnabled = true, Name = d.Brand, Children = new List<PropertyNodeItem>() });
                node.Children.Add(new PropertyNodeItem { Tag = d, IsEnabled = d.Num > 0, IsAdd = d.Num > 0 ? Visibility.Visible : Visibility.Collapsed, Name = d.Name, Value = d, Icon = "image/2.png" });
            });
            treeView.ItemsSource = tree;
            treeView.SelectedItemChanged += TreeView_SelectedItemChanged;

            List<PrintInfo> pinfos = PrintLogic.GetPrints();
            Combobox1.ItemsSource = pinfos;
            Combobox1.DisplayMemberPath = "Name";
            Combobox1.SelectionChanged += Combobox1_SelectionChanged;

            if (treeView.ItemContainerGenerator.ContainerFromIndex(0) is TreeViewItem item)
            {
                item.IsExpanded = true;
                item.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
            }
        }

        private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
        {
            if (sender is ItemContainerGenerator items && items.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
                if (items.ContainerFromIndex(0) is TreeViewItem item)
                    item.Focus();
        }

        private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (!(treeView.SelectedItem is PropertyNodeItem item) || item.Tag == null)
                return;

            if (!(item.Tag is DevicesInformation info))
                return;

            IPrint print = PrintLogic.GetPrintConfig(info);
            if (!(Combobox1.ItemsSource is List<PrintInfo> printlist))
                return;

            PrintInfo pinfo = printlist.FirstOrDefault(o=>o.Name==print.PrintInfo.Name);
            Combobox1.SelectedItem = pinfo;
            Combobox1_SelectionChanged(null, null);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (!(treeView.SelectedItem is PropertyNodeItem item) || item.Tag == null)
                return;

            if (!(item.Tag is DevicesInformation info))
                return;

            PrintInfo print = Combobox1.SelectedItem as PrintInfo;
            PrintLogic.SavePrintConfig(info, print);
            BCL.WPFComponent.UCMessageBox.Show("更换模板成功");
        }
    }


    public class PropertyNodeItem
    {
        public string Icon { get; set; }
        public string EditIcon { get; set; }
        public string DisplayName { get; set; }
        public string Name { get; set; }
        public bool IsEnabled { get; set; }
        public Visibility IsAdd { get; set; }
        public DevicesInformation Value { get; set; }
        public List<PropertyNodeItem> Children { get; set; }
        public object Tag { get; set; }
        public PropertyNodeItem()
        {
            Icon = "image/1.png";
            IsAdd = Visibility.Collapsed;
        }

    }
}
